package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.entity.ExperienceTicket;
import com.web.exiaodao.business.entity.ExperienceTicketRecord;
import com.web.exiaodao.remote.vo.ExperienceTicketRecordVo;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.ExperienceTicketRecordRepository;
import com.web.exiaodao.business.repository.ExperienceTicketRepository;
import com.web.exiaodao.remote.service.ApiExperienceTicketRecordService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2016-12-28 10:05:04
 * 类的说明：前端APIExperienceTicketRecord接口实现类
 **/
@Service("ApiExperienceTicketRecordService")
@Transactional(readOnly = false)
public class ApiExperienceTicketRecordServiceImpl extends BaseService<ExperienceTicketRecord> implements ApiExperienceTicketRecordService{

	private static final Logger LOGGER = Logger.getLogger(ApiExperienceTicketRecordServiceImpl.class);

	@Autowired
	ExperienceTicketRecordRepository experienceTicketRecordRepository;
	@Autowired
	ExperienceTicketRepository experienceTicketRepository;
	
	@Override
	public XaResult<ExperienceTicketRecordVo> findOne(Long tId) throws BusinessException {
		ExperienceTicketRecord obj = experienceTicketRecordRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		XaResult<ExperienceTicketRecordVo> xr = new XaResult<ExperienceTicketRecordVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),ExperienceTicketRecordVo.class));
			xr.getObject().setId(obj.getId());
			LOGGER.info("查询单个实体----->ExperienceTicketRecord");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<ExperienceTicketRecordVo> findLastOne() throws BusinessException {
		XaResult<ExperienceTicketRecordVo> xr = new XaResult<>();
		Map<String, SearchFilter> filters = new HashMap<>();
			filters.put("status", new SearchFilter("status", Operator.NE, XaConstant.Status.delete));
			List<ExperienceTicketRecord> entitys = experienceTicketRecordRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ExperienceTicketRecord.class), new Sort(
				new Sort.Order[]{
				new Sort.Order(Sort.Direction.DESC, "createTime"),
				new Sort.Order(Sort.Direction.DESC, "id")
				}));
				if (entitys == null || entitys.size() == 0) {
				xr.error("暂无信息");
				} else {
				xr.setObject(JSON.parseObject(JSON.toJSONString(entitys.get(0)), ExperienceTicketRecordVo.class));
				xr.getObject().setId(entitys.get(0).getId());
				LOGGER.info("查询单个实体----->ExperienceTicketRecord");
			}
		return xr;
	}

	@Override
	public XaResult<List<ExperienceTicketRecordVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ExperienceTicketRecord> page = experienceTicketRecordRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ExperienceTicketRecord.class), pageable);
		XaResult<List<ExperienceTicketRecordVo>> xr = new XaResult<List<ExperienceTicketRecordVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ExperienceTicketRecordVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<ExperienceTicketRecordVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ExperienceTicketRecord> page = experienceTicketRecordRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ExperienceTicketRecord.class), pageable);
		XaResult<List<ExperienceTicketRecordVo>> xr = new XaResult<List<ExperienceTicketRecordVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ExperienceTicketRecordVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<ExperienceTicketRecordVo> multiOperate(String modelIds,
			Integer status) throws BusinessException {
		XaResult<ExperienceTicketRecordVo> xr = new XaResult<ExperienceTicketRecordVo>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ExperienceTicketRecord obj = experienceTicketRecordRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = experienceTicketRecordRepository.save(obj);
					xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ExperienceTicketRecordVo.class));
					xr.getObject().setId(obj.getId());
				}
			}
		}
		LOGGER.info("更改数据状态----->ExperienceTicketRecord");
		return xr;
	}

	@Override
	public XaResult<ExperienceTicketRecordVo> createModel(ExperienceTicketRecord model)
			throws BusinessException {
		XaResult<ExperienceTicketRecordVo> xr = new XaResult<ExperienceTicketRecordVo>();
		ExperienceTicketRecord obj = experienceTicketRecordRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ExperienceTicketRecordVo.class));
		xr.getObject().setId(obj.getId());
		//库存-1
		ExperienceTicket tobj = experienceTicketRepository.findOne(model.getExperienceTicketId());
		if(XaUtil.isNotEmpty(tobj)){
			Long stock = tobj.getStock();
			tobj.setStock(stock-1);
			experienceTicketRepository.save(tobj);
		}
		LOGGER.info("新增数据成功----->ExperienceTicketRecord");
		return xr;
	}

	@Override
	public XaResult<ExperienceTicketRecordVo> updateModel(ExperienceTicketRecord model)
			throws BusinessException {
		ExperienceTicketRecord obj = experienceTicketRecordRepository.findOne(model.getId());
		XaResult<ExperienceTicketRecordVo> xr = new XaResult<ExperienceTicketRecordVo>();
		if (XaUtil.isNotEmpty(obj)) {
					obj.setUserId(XaUtil.isNotEmpty(model.getUserId()) ? model.getUserId() : obj.getUserId());
					obj.setExperienceTicketId(XaUtil.isNotEmpty(model.getExperienceTicketId()) ? model.getExperienceTicketId() : obj.getExperienceTicketId());
				obj = experienceTicketRecordRepository.save(obj);
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ExperienceTicketRecordVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			xr.error(XaConstant.Message.object_not_find);
		}
		LOGGER.info("编辑数据成功----->ExperienceTicketRecord");
		return xr;
	}

}
